import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/market_model/market_incubation_dynamic_list_model.dart';
import 'package:yysk_app/net/api/market_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/empty_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';

class MarketTransactionDynamicsPage extends StatefulWidget {
  static const path = '/MarketTransactionDynamics';
  const MarketTransactionDynamicsPage({super.key});
  @override
  State<StatefulWidget> createState() => _MarketTransactionDynamicsPage();
}

class _MarketTransactionDynamicsPage
    extends State<MarketTransactionDynamicsPage> {
  final ScrollController scrollController = ScrollController();
  var limarketDetailst = RxList<MarketIncubationDynamicListItem>([]);
  var id = '';
  int pageNum = 1;
  int pageSize = 10;
  @override
  void initState() {
    super.initState();
    id = Get.parameters['id'] ?? Get.arguments['id'] ?? '0';
    getlist();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        getlist(loadMore: true);
      }
    });
  }

  Future<void> getlist({loadMore = false}) async {
    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      http.netFetch<MarketIncubationDynamicListModel>(
        MarketUrl.getIncubationDynamic,
        params: {'pageNum': pageNum, 'pageSize': 20, 'collectionId': id},
        method: Method.post,
        success: (result) {
          List<MarketIncubationDynamicListItem> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            limarketDetailst.clear();
          }
          limarketDetailst.addAll(items);
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNum--;
      }
    }
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      body: Stack(
        children: [
             Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0.px, top, 0.px, 0.px),
            child: Obx(
              () => Column(
                children: [
                  CustomAppBar2(title: '交易动态', context: context),

                  Expanded(
                    child: RefreshIndicator(
                      backgroundColor: Colors.transparent,
                      color: Colors.white,
                      onRefresh: _handleRefresh,
                      child: _buildPlatformList(
                        list: limarketDetailst,
                        scrollController: scrollController,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _handleRefresh() async {
    getlist();
    return;
  }

  Widget _buildPlatformList({
    required ScrollController scrollController,
    required List<MarketIncubationDynamicListItem> list,
  }) {
    return list.isEmpty
        ? const EmptyWidget()
        : ListView.builder(
          controller: scrollController,
          padding: EdgeInsets.only(
            left: 12.px,
            right: 12.px,
            top: 10.px,
            bottom: 10.px,
          ),
          itemCount: list.length,
          physics: const AlwaysScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return _item(item: list[index]);
          },
        );
  }

  _item({required MarketIncubationDynamicListItem item}) {
    return Container(
      width: 351.px,
      margin: EdgeInsets.only(bottom: 8.px),
      padding: EdgeInsets.fromLTRB(12.px, 12.px, 12.px, 12.px),
      decoration: BoxDecoration(
        color: AppColors.backgroundColorGrey2,
        borderRadius: BorderRadius.circular(6.px),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  ImageAssetWidget(
                    width: 18.px,
                    height: 18.px,
                    imageUrl: 'static/images/market/get.png',
                  ),
                  SizedBox(width: 8.px),
                  Text(
                    '#${item.tokenId}',
                    style: TextStyle(
                      color: AppColors.backgroundColor,
                      fontSize: 14.px,
                    ),
                  ),
                ],
              ),
              Text(
                '${item.orderPrice}',
                style: TextStyle(
                  color: AppColors.backgroundColor,
                  fontSize: 14.px,
                ),
              ),
            ],
          ),
          SizedBox(height: 8.px),
          Row(
            children: [
              SizedBox(width: 28.px),
              Text(
                '${item.createTime}',
                style: TextStyle(
                  color: AppColors.backgroundColorGrey,
                  fontSize: 13.px,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
